using Common.Class;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Common.Base
{
    public class XucBaseAdd : XucBase
    {
        private IContainer components = null;
        protected GroupControl gcInfo;
        protected TextEdit txtNAME;
        protected LabelControl lblName;
        protected TextEdit txtID;
        protected LabelControl lblId;
        protected ImageCollection imageCollection2;
        protected DXErrorProvider Err;
        protected SimpleButton btnSave;
        protected SimpleButton btnCancel;
        protected SimpleButton btnSaveNew;
        public event ButtonClickEventHander CancelClick;
        protected override void Dispose(bool disposing)
        {
            if (disposing && this.components != null)
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(XucBaseAdd));
            SuperToolTip superToolTip = new SuperToolTip();
            ToolTipItem toolTipItem = new ToolTipItem();
            SuperToolTip superToolTip2 = new SuperToolTip();
            ToolTipItem toolTipItem2 = new ToolTipItem();
            this.gcInfo = new GroupControl();
            this.txtNAME = new TextEdit();
            this.lblName = new LabelControl();
            this.txtID = new TextEdit();
            this.lblId = new LabelControl();
            this.imageCollection2 = new ImageCollection(this.components);
            this.Err = new DXErrorProvider(this.components);
            this.btnSave = new SimpleButton();
            this.btnCancel = new SimpleButton();
            this.btnSaveNew = new SimpleButton();
            ((ISupportInitialize)this.gcInfo).BeginInit();
            this.gcInfo.SuspendLayout();
            ((ISupportInitialize)this.txtNAME.Properties).BeginInit();
            ((ISupportInitialize)this.txtID.Properties).BeginInit();
            ((ISupportInitialize)this.imageCollection2).BeginInit();
            ((ISupportInitialize)this.Err).BeginInit();
            base.SuspendLayout();
            this.gcInfo.Controls.Add(this.txtNAME);
            this.gcInfo.Controls.Add(this.lblName);
            this.gcInfo.Controls.Add(this.txtID);
            this.gcInfo.Controls.Add(this.lblId);
            this.gcInfo.Location = new Point(1, 1);
            this.gcInfo.Name = "gcInfo";
            this.gcInfo.Size = new Size(383, 55);
            this.gcInfo.TabIndex = 2;
            this.gcInfo.Text = "Thông tin bắt buộc";
            this.txtNAME.Location = new Point(150, 27);
            this.txtNAME.Name = "txtNAME";
            this.txtNAME.Size = new Size(223, 20);
            this.txtNAME.TabIndex = 3;
            this.txtNAME.EditValueChanged += new EventHandler(this.TxtNameEditValueChanged);
            this.txtNAME.Leave += new EventHandler(this.TxtNameLeave);
            this.lblName.Location = new Point(126, 30);
            this.lblName.Name = "lblName";
            this.lblName.Size = new Size(18, 13);
            this.lblName.TabIndex = 2;
            this.lblName.Text = "Tên";
            this.txtID.Location = new Point(27, 26);
            this.txtID.Name = "txtID";
            this.txtID.Size = new Size(93, 20);
            this.txtID.TabIndex = 1;
            this.txtID.EditValueChanged += new EventHandler(this.TxtIDEditValueChanged);
            this.txtID.Leave += new EventHandler(this.TxtIDLeave);
            this.txtID.KeyDown += new KeyEventHandler(this.TxtIDKeyDown);
            this.lblId.Location = new Point(6, 29);
            this.lblId.Name = "lblId";
            this.lblId.Size = new Size(14, 13);
            this.lblId.TabIndex = 0;
            this.lblId.Text = "Mã";
            // ReSharper disable once ResourceItemNotResolved
            //this.imageCollection2.ImageStream = (ImageCollectionStreamer)componentResourceManager.GetObject("imageCollection2.ImageStream");
            this.Err.ContainerControl = this;
            this.btnSave.ImageIndex = 0;
            this.btnSave.ImageList = this.imageCollection2;
            this.btnSave.Location = new Point(89, 78);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(88, 27);
            toolTipItem.Text = "Ctrl+S\r\n";
            superToolTip.Items.Add(toolTipItem);
            this.btnSave.SuperTip = superToolTip;
            this.btnSave.TabIndex = 35;
            this.btnSave.Text = "Lưu && Đóng";
            this.btnSave.Visible = false;
            this.btnSave.Click += new EventHandler(this.BtnSaveClick);
            this.btnSave.KeyDown += new KeyEventHandler(this.BtnSaveKeyDown);
            this.btnCancel.ImageIndex = 6;
            this.btnCancel.ImageList = this.imageCollection2;
            this.btnCancel.Location = new Point(276, 78);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(95, 27);
            this.btnCancel.TabIndex = 36;
            this.btnCancel.Text = "Đóng";
            this.btnCancel.Click += new EventHandler(this.BtnCancelClick);
            this.btnCancel.KeyDown += new KeyEventHandler(this.BtnCancelKeyDown);
            this.btnSaveNew.ImageIndex = 0;
            this.btnSaveNew.ImageList = this.imageCollection2;
            this.btnSaveNew.Location = new Point(183, 78);
            this.btnSaveNew.Name = "btnSaveNew";
            this.btnSaveNew.Size = new Size(87, 27);
            toolTipItem2.Text = "Ctrl+Shift +S";
            superToolTip2.Items.Add(toolTipItem2);
            this.btnSaveNew.SuperTip = superToolTip2;
            this.btnSaveNew.TabIndex = 37;
            this.btnSaveNew.Text = "Lưu";
            this.btnSaveNew.Click += new EventHandler(this.BtnSaveNewClick);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            //base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.btnSaveNew);
            base.Controls.Add(this.btnSave);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.gcInfo);
            base.Name = "XucBaseAdd";
            base.Size = new Size(386, 108);
            ((ISupportInitialize)this.gcInfo).EndInit();
            this.gcInfo.ResumeLayout(false);
            this.gcInfo.PerformLayout();
            ((ISupportInitialize)this.txtNAME.Properties).EndInit();
            ((ISupportInitialize)this.txtID.Properties).EndInit();
            ((ISupportInitialize)this.imageCollection2).EndInit();
            ((ISupportInitialize)this.Err).EndInit();
            base.ResumeLayout(false);
        }
        public XucBaseAdd()
        {
            this.InitializeComponent();
            this.Init();
        }
        protected virtual void Init()
        {
            this.ReLoad();
        }
        public void SetData(string id)
        {
        }
        protected virtual bool Check()
        {
            return true;
        }
        public virtual void Save()
        {
            if (this.txtID.ErrorText != string.Empty | this.txtID.Text == string.Empty)
            {
                XtraMessageBox.Show((this.txtID.ErrorText.Length == 0) ? "Dữ liệu của ô này không được bỏ trống." : this.txtID.ErrorText, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtID.Focus();
            }
            else
            {
                if (this.txtNAME.ErrorText != string.Empty | this.txtNAME.Text == string.Empty)
                {
                    XtraMessageBox.Show((this.txtNAME.ErrorText.Length == 0) ? "Dữ liệu của ô này không được bỏ trống." : this.txtNAME.ErrorText, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.txtNAME.Focus();
                }
                else
                {
                    if (this.Check())
                    {
                        switch (this.MStatus)
                        {
                            case Actions.Add:
                                this.UcSave();
                                break;

                            case Actions.Update:
                                this.UcUpdate();
                                break;
                        }
                    }
                }
            }
        }
        public virtual void Delete()
        {
            this.UcDelete();
        }
        public virtual void ReLoad()
        {
        }
        protected virtual string UcSave()
        {
            return string.Empty;
        }
        protected virtual string UcUpdate()
        {
            return string.Empty;
        }
        protected virtual string UcDelete()
        {
            return string.Empty;
        }
        protected virtual void BtnSaveClick(object sender, EventArgs e)
        {
            this.MCloseOrNew = CloseOrNew.Close;
            this.Save();
        }
        protected virtual void BtnCancelClick(object sender, EventArgs e)
        {
            this.RaiseCancelClickEventHander();
        }
        protected virtual void BtnSaveKeyDown(object sender, KeyEventArgs e)
        {
            this.BtnSaveClick(this.btnSave, e);
        }
        protected virtual void BtnCancelKeyDown(object sender, KeyEventArgs e)
        {
            this.BtnCancelClick(this.btnCancel, e);
        }
        private void BtnSaveNewClick(object sender, EventArgs e)
        {
            this.Save();
            if (this.MStatus == Actions.Add)
            {
                this.txtID.Properties.ReadOnly = false;
                this.txtID.Focus();
                this.Add();
            }
        }
        public virtual void Add()
        {
        }
        private void TxtIDLeave(object sender, EventArgs e)
        {
            TextEdit textEdit = (TextEdit)sender;
            if (this.MStatus != Actions.None)
            {
                if (textEdit.Text == string.Empty)
                {
                    this.Err.SetError(textEdit, "Vui lòng nhập thông tin." + this.Text);
                }
            }
        }
        protected virtual void TxtIDEditValueChanged(object sender, EventArgs e)
        {
        }
        protected virtual void TxtIDKeyDown(object sender, KeyEventArgs e)
        {
        }
        private void TxtNameLeave(object sender, EventArgs e)
        {
            TextEdit textEdit = (TextEdit)sender;
            if (this.MStatus != Actions.None)
            {
                if (textEdit.Text == string.Empty)
                {
                    this.Err.SetError(textEdit, "Vui lòng nhập thông tin." + this.Text);
                }
                if (string.IsNullOrEmpty(this.txtID.Text))
                {
                    if (!string.IsNullOrEmpty(this.txtNAME.Text))
                    {
                        this.txtID.Text = base.GenerateCode(this.txtNAME.Text);
                    }
                }
            }
        }
        private void TxtNameEditValueChanged(object sender, EventArgs e)
        {
            TextEdit textEdit = (TextEdit)sender;
            if (this.MStatus != Actions.None)
            {
                if (textEdit.ErrorText != string.Empty)
                {
                    this.Err.SetError(textEdit, string.Empty);
                }
            }
        }
        public void RaiseCancelClickEventHander()
        {
            if (this.CancelClick != null)
            {
                this.CancelClick(this);
            }
        }
    }
}